<template>
  <Overlay
    :show="show"
    :overlay="overlay"
    :overlayClass="overlayClass"
    :closeOnClickOverlay="false"
    @update:show="updateShow"
  />

  <transition name="fade">
    <template v-if="visible">
      <view
        v-show="show"
        :class="['lxd-dialog', { 'lxd-dialog--round': round }, `lxd-dialog--${top ? 'top' : 'center'}`]"
      >
        <view class="lxd-dialog__header" v-if="title">{{ title }}</view>

        <view class="lxd-dialog__content">
          <view class="lxd-dialog__message" v-if="message" v-html="message"></view>
          <view class="lxd-dialog__message" v-else>
            <slot></slot>
          </view>
        </view>

        <view class="lxd-dialog__footer">
          <view class="lxd-button lxd-button__cancel" @click="handleCancel">{{ cancelText }}</view>
          <view class="lxd-button lxd-button__primary" @click="handleConfirm">
            {{ confirmText }}
          </view>
        </view>
      </view>
    </template>
  </transition>
</template>

<script lang="ts" setup>
import Overlay from "../Overlay/index.vue";
import { ref, watch } from "vue";

interface Props {
  show: boolean;
  title?: string;
  message?: string;
  overlay?: boolean;
  overlayClass?: string;
  top?: string;
  round?: boolean;
  closeOnClickOverlay?: boolean;
  className?: string;
  cancelText?: string;
  confirmText?: string;
  showConfirmButton?: boolean;
  showCancelButton?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  show: false, // 是否显示弹出层
  title: "", // 标题
  message: "", // 文本内容
  overlay: true, // 是否显示遮罩层
  overlayClass: "", // 自定义遮罩层类名
  top: "", //上下位置
  round: false, // 是否显示圆角
  closeOnClickOverlay: true, // 是否在点击遮罩层后关闭
  className: "", //自定义类名
  cancelText: "取消", // 取消按钮文案
  confirmText: "确定", // 确认按钮文案
  showConfirmButton: true, //	是否展示确认按钮
  showCancelButton: false // 是否展示取消按钮
});

const emits = defineEmits(["update:show", "cancel", "confirm"]);

const visible = ref(false);

const watchOne = watch(
  () => props.show,
  (s) => {
    s && (visible.value = true);
    watchOne();
  }
);

const updateShow = (value: boolean) => {
  emits("update:show", value);
};

const handleCancel = () => {
  emits("cancel");
};

const handleConfirm = () => {
  emits("confirm");
};
</script>

<style lang="scss" scoped>
.lxd-dialog {
  position: fixed;
  z-index: 1997;
  width: 600rpx;
  background-color: #fff;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
  text-align: center;
  line-height: 1;
}

.lxd-dialog--round {
  border-radius: 20rpx;
}

.lxd-dialog--center {
  top: 50%;
}

.lxd-dialog--top {
  top: v-bind(top);
}

.lxd-dialog-container {
  border-radius: 12px;
  background-color: white;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  width: 100%;
  padding-top: 42rpx;
}

.lxd-dialog__header {
  padding-top: 42rpx;
  font-size: 34rpx;
  font-weight: bold;
  letter-spacing: 1rpx;
}

.lxd-dialog__content {
  padding: 16rpx 0;
  min-height: 140rpx;
  display: flex;
  align-items: center;
  line-height: 1.5;
}

.lxd-dialog__message {
  flex: 1;
  max-height: 60vh;
  overflow-y: auto;
  padding: 30rpx 20rpx;
  white-space: pre-wrap;
  text-align: center;
  word-wrap: break-word;
}

.lxd-dialog__footer {
  display: flex;
  justify-content: flex-end;
  justify-content: space-between;
  border-top: 1px solid #f4f4f4;
  letter-spacing: 1rpx;
  font-size: calc(var(--amplified_base) * 30rpx);

  .lxd-button {
    padding: 14px 0;
    flex: 1;
  }

  .lxd-button__cancel {
    border-right: 1px solid #f4f4f4;
  }

  .lxd-button__primary {
    color: var(--subject_color);
  }
}

// .lxd-button {
//   margin-left: 10px;
//   padding: 8px 20px;
//   border: none;
//   border-radius: 4px;
//   background-color: #f4f4f5;
//   color: #606266;
//   cursor: pointer;

//   &--primary {
//     background-color: #409eff;
//     color: white;
//   }
// }

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
